let express = require('express');
let app = express();
app.listen(3000, () => {
    console.log('服务器已启动。。。');
});
// 引入封装模块
let DB = require('./packge/db')

// 引入post中间件
app.use(express.urlencoded({ extended: true }))
app.use(express.json())

// 引入跨域中间件
let cors = require('cors');
app.use(cors());

app.post('/index', async (req, res, next) => {
    let username = req.body.username;
    let sql = `select * from userlist where username = "${username}"`
    let [err,result] = await DB(sql);
    // 错误拦截
    console.log(err);
    if(err){
        next('错了')
        return;
    }
    if(result.length>0){
        next('已经有了')
        return;
    }
    res.send({
        status:200,
        msg:'注册成功'
    })
})

// 逻辑错误
app.use((err, req, res, next) => {
    res.send({
        status: 500,
        msg: err
    })
})
